package com.zhouxin.customview.extensions

import android.content.res.Resources
import android.util.TypedValue

/**
 * Copyright©
 * FileName: Extensions
 * Author: ZhouXin
 * Description: 屏幕相关的扩展
 */


/*-------------------------------------------Float扩展-------------------------------------------*/
private val displayMetrics = Resources.getSystem().displayMetrics
/**
 * dip转px
 */
val Float.dp2px
    @JvmName("dp2px")
    get() = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        this,
        displayMetrics
    )

/**
 * px转dip
 */
val Float.px2dp
    @JvmName("px2dp")
    get() = this / displayMetrics.density


/**
 * sp转px
 */
val Float.sp2px
    @JvmName("sp2px")
    get() = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_SP,
        this,
        displayMetrics
    )

/**
 * px转sp
 */
val Float.px2sp
    @JvmName("px2sp")
    get() = this / displayMetrics.scaledDensity


/*-------------------------------------------Int扩展-------------------------------------------*/
val Int.dp2px
    @JvmName("dp2px")
    get() = this.toFloat().dp2px

val Int.px2dp
    @JvmName("px2dp")
    get() = this.toFloat().px2dp

val Int.sp2px
    @JvmName("sp2px")
    get() = this.toFloat().sp2px

val Int.px2sp
    @JvmName("px2sp")
    get() = this.toFloat().px2sp